//
//  ApplicationRecommendViewController.m
//  Molian
//
//  Created by 胡津 on 9/7/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "ApplicationRecommendViewController.h"
#import "MLLoginTextField.h"

@interface ApplicationRecommendViewController ()<UITextViewDelegate>
@property BOOL isSubmit;//是否提交过了
@property (nonatomic, strong)MLAuthentication *loveExperience;
@end

@implementation ApplicationRecommendViewController
{
    MLLoginTextField*phonenumber;
    UILabel *_placeholderLabel;
    UITextView *gushi;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.isSubmit = NO;
    // Do any additional setup after loading the view.
    self.title = @"成为梦想恋人";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [AVAnalytics event:@"申请成为梦想恋人"]; // 通知服务器一个点击事件。

    //手机号码文本框
    phonenumber = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 80, DeviceWidth, 40)];
    phonenumber.placeholder = @"填写您的QQ";
    phonenumber.keyboardType = UIKeyboardTypePhonePad;
    phonenumber.leftPrompt = @"  联系方式  ";
    [self.view addSubview:phonenumber];
    phonenumber.backgroundColor =[UIColor whiteColor];
    [phonenumber style];
    phonenumber.line.hidden = YES;
    
    
    gushi = [[UITextView alloc]initWithFrame:CGRectMake(0, 140, DeviceWidth, 200)];
    gushi.delegate = self;
    gushi.font = [UIFont fontWithName:FontName size:15];
    [self.view addSubview:gushi];
    
    
    _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 145, CGRectGetWidth(gushi.frame)-10, 40)];
    _placeholderLabel.text = @"向小编描述你的故事,说出你的经历和特长方便我们了解你(不低于50字)";
    _placeholderLabel.textColor = [UIColor grayColor];
    _placeholderLabel.numberOfLines = 3;
    _placeholderLabel.textAlignment = NSTextAlignmentLeft;
    _placeholderLabel.font = [UIFont fontWithName:FontName size:15];
    [self.view addSubview:_placeholderLabel];
    
    UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(saveUserInfo)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyBorad)]];
    
    self.loveExperience =[MLAuthentication object];
    _loveExperience.sponsor = [MLUser currentUser];
    _loveExperience.status = AuthenticationStatus_APPLYING;
    _loveExperience.needRecommend = MLAuthenticationNeedRecommend_YES;
    
    //检查是否提交过
    [self inspectSubmit];
}
//检查是否提交过(正在审核)
- (void)inspectSubmit
{
    AVQuery *q =[MLAuthentication query];
    [q whereKey:@"sponsor" equalTo:[MLUser currentUser]];
    [q whereKey:@"needRecommend" equalTo:[NSNumber numberWithInteger:MLAuthenticationNeedRecommend_YES]];
    [q whereKey:@"status" equalTo:[NSNumber numberWithInteger:AuthenticationStatus_APPLYING]];
    [q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error&&objects.count) {
            self.isSubmit = YES;
            [HUD Prompt:@"已有申请资料在审核中"];
        }
    }];
}
//回收键盘
-(void)returnKeyBorad
{
    [self.view endEditing:YES];
}

-(void)saveUserInfo
{
    self.navigationItem.rightBarButtonItem.enabled = NO;
    if (self.isSubmit == NO) {
        if(phonenumber.text&&phonenumber.text.length>0&&gushi.text&&gushi.text.length>=50){
            //再次检查
            
            AVQuery *q =[MLAuthentication query];
            [q whereKey:@"sponsor" equalTo:[MLUser currentUser]];
            [q whereKey:@"status" equalTo:[NSNumber numberWithInteger:AuthenticationStatus_APPLYING]];
            [q whereKey:@"needRecommend" equalTo:[NSNumber numberWithInteger:MLAuthenticationNeedRecommend_YES]];
            [q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error&&objects.count) {
                    self.navigationItem.rightBarButtonItem.enabled = YES;
                    
                    self.isSubmit = YES;
                    [HUD Prompt:@"已有申请资料在审核中"];
                }else{
                    //可以提交
                    self.loveExperience.needRecommend = MLAuthenticationNeedRecommend_YES;
                    self.loveExperience.contact = phonenumber.text;
                    self.loveExperience.story = gushi.text;
                    [self.loveExperience saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        if (succeeded) {
                            self.navigationItem.rightBarButtonItem.enabled = YES;
                            [self.navigationController popViewControllerAnimated:YES];
                            [HUD Prompt:@"提交成功"];
                        }else{
                            self.navigationItem.rightBarButtonItem.enabled = YES;
                            [HUD Prompt:@"提交失败"];
                        }
                        
                    }];
                }
            }];
            
        }else{
            self.navigationItem.rightBarButtonItem.enabled = YES;
            [HUD Prompt:@"信息不完整不能提交哦"];
        }
    }else{
        self.navigationItem.rightBarButtonItem.enabled = YES;
        [HUD Prompt:@"已有申请资料在审核中"];
    }
    
    

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ( [text isEqualToString:@"\n"] ) {
        [textView resignFirstResponder];
        return NO;
    }
    
    if (![text isEqualToString:@""])
    {
        _placeholderLabel.hidden = YES;
    }
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
    {
        _placeholderLabel.hidden = NO;
    }
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
